package com.document;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.DBHelper.MySQLIteOpenHelper;
//调用相机拍照，创建PDF类
public class Creat_By_yCamera extends MyActivity {

	private Button yestocreat__;
	private Button Continue;
	private Button backtomain;
	private EditText filaname_;
	String fileName;
	String pdfUrl;
	ArrayList<String> imageUrllist=new ArrayList<String>();
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.creat_by_camera);
		yestocreat__=(Button)findViewById(R.id.yestocreat__);
		Continue=(Button)findViewById(R.id.continue_);
		backtomain=(Button)findViewById(R.id.backtomain);
		filaname_=(EditText)findViewById(R.id.filename__);
		//确定创建
		yestocreat__.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				MySQLIteOpenHelper helper=new MySQLIteOpenHelper(getApplicationContext());
				String selectsql="select * from FileName";
				//查询数据库数据 得到文件路径集合
				imageUrllist=helper.select(selectsql);
				
				String filename=filaname_.getText().toString();
				 pdfUrl = GetPath.getpath()+File.separator+filename+".pdf";
				if(!filename.equals("")){
				com.CreatPDF.CreatPdfFromImg.Pdf(imageUrllist, pdfUrl);
				File mPdfFile = new File(pdfUrl);	
				//创建完成后，清空数据表
				 helper.delete("delete from FileName");
				 helper.close();
				Toast.makeText(getApplicationContext(), "创建文件成功，可以在"+GetPath.getpath()+File.separator+"路径下查看", Toast.LENGTH_LONG).show();
				
				}
				else{
					Toast.makeText(getApplicationContext(), "创建文件失败，请输入文件名称", Toast.LENGTH_LONG).show();
				}		
			}
		});
		
		//拍照,调用系统相机
		Continue.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				 //以当前时间为图片名称 获取当前系统时间
		           SimpleDateFormat formatter=new SimpleDateFormat("MM月dd日HH.mm.ss");       
		       	Date curDate=new Date(System.currentTimeMillis());//获取当前时间       
		       	String str=formatter.format(curDate);
		         fileName = "mnt/sdcard/DCIM/PDF"+str+".jpg";
				//启用系统相机程序				 
				Intent intent=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
				File f=new File(fileName);
				Uri u=Uri.fromFile(f);
				intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
				intent.putExtra(MediaStore.EXTRA_OUTPUT, u);
				startActivityForResult(intent,1);               				
			}
		});
		backtomain.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {				
				System.out.println("pdfUrl   "+pdfUrl);
				 if(pdfUrl!=null)
				 {
					 MySQLIteOpenHelper helper=new MySQLIteOpenHelper(getApplicationContext());
				//离开界面，清空数据表
				 helper.delete("delete from FileName");
				 helper.close();
				 startActivity(getFileIntent.getPdfFileIntent(pdfUrl));
				 finish();}
				 else{
					Toast.makeText(Creat_By_yCamera.this, "你还没创建文件", Toast.LENGTH_LONG).show();
					
				 }
				
				
			}
		});
	
		
	}
	//回调方法，返回照片数据
	   @Override
	    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	        super.onActivityResult(requestCode, resultCode, data);

	        if (resultCode == Activity.RESULT_OK) {

	            String sdStatus = Environment.getExternalStorageState();
	            if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用
	                return;
	            }

	            
	            Bitmap bitmap = BitmapFactory.decodeFile(fileName);// 获取相机返回的数据，并转换为Bitmap图片格式
	           // getbitmap(bitmap);
	            ((ImageView) findViewById(R.id.image)).setImageBitmap(bitmap);
	            //拍照成功，提示继续拍照
	            Toast.makeText(getApplicationContext(), "生成图片成功，可以继续拍照，加入PDF文档", Toast.LENGTH_LONG).show();
	            MySQLIteOpenHelper helper=new MySQLIteOpenHelper(getApplicationContext());
	            //将信息加入数据库
	 			String insertsql="insert into FileName values('"+fileName+"')";
	 			helper.insert(insertsql);
	 			helper.close();
	        }
	    }
	   
	

		   
	   }

